package view;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.MutableTreeNode;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Image;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.DefaultMutableTreeNode;

import control.Control;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainWindow extends JFrame implements KeyListener {
	private static final int COVER_WIDTH=300;
	private static final int COVER_HEIGHT=300;
	private static final int MINIMUM_WINDOW_WIDTH=500;
	private static final int MINIMUM_WINDOW_HEIGHT=630;
	private static final int DEFAULT_WINDOW_WIDTH=500;
	private static final int DEFAULT_WINDOW_HEIGHT=630;
	private static final int TEXTFIELDCOLUMNS=25;
	
	private JTree tree;
	private JPanel infoPanel;
	private JPanel textPanel;
	private JTextField songTitle ;
	private JTextField interpret ;
	private JTextField album ;
	private JTextField year ;
	private JButton frontCover;
	private Control control;
	
	
	
	protected MainWindow(MutableTreeNode topNode){
		control=Control.getControl();
		this.setMinimumSize(new Dimension(MINIMUM_WINDOW_WIDTH,MINIMUM_WINDOW_HEIGHT));
		this.setSize(DEFAULT_WINDOW_WIDTH,DEFAULT_WINDOW_HEIGHT);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setLayout(new BorderLayout());
		
		this.setTitle("ID3-Tag-Editor");
		
		setTree(topNode);
		
		infoPanel=new JPanel();
		this.getContentPane().add(infoPanel,BorderLayout.EAST);
		infoPanel.setLayout(new BorderLayout());
		textPanel=new JPanel();
		infoPanel.add(textPanel,BorderLayout.NORTH);
		textPanel.setLayout(new GridLayout(9,1));
		textPanel.add(new JLabel("Titel:"));
		songTitle=new JTextField();
		songTitle.setColumns(TEXTFIELDCOLUMNS);
		textPanel.add(songTitle);
		textPanel.add(new JLabel("Interpret:"));
		interpret=new JTextField();
		interpret.setColumns(TEXTFIELDCOLUMNS);
		textPanel.add(interpret);
		textPanel.add(new JLabel("Album:"));
		album=new JTextField();
		album.setColumns(TEXTFIELDCOLUMNS);
		textPanel.add(album);
		textPanel.add(new JLabel("Jahr:"));
		year=new JTextField();
		year.addKeyListener(this);
		year.setColumns(TEXTFIELDCOLUMNS);
		textPanel.add(year);
		JPanel buttonPanel=new JPanel();
		textPanel.add(buttonPanel);
		buttonPanel.setLayout(new GridLayout(1,2));
		JButton saveButton=new JButton("Speichern");
		saveButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				control.enteredData();
			}
		});
		buttonPanel.add(new JPanel());
		buttonPanel.add(saveButton);
		buttonPanel.setBorder(new EmptyBorder(5,5,0,0));
		textPanel.add(buttonPanel);
		this.setVisible(true);
	}
	
	//used to delete the last character of the year textfield if its no digit
	//or if the year is longer than 4 digits
	protected void correctYearFormat(){
		if (year.getText().length()>0){
			if (!Character.isDigit(year.getText().charAt(year.getText().length()-1))){
				try{
					year.setText(year.getText(0,year.getText().length()-1));	
				}
				catch (Exception e) {
					System.out.println("Error (YearFormat)");
				}
			}
			if (year.getText().length()>4){
				try{
					year.setText(year.getText(0,year.getText().length()-1));	
				}
				catch (Exception e) {
					System.out.println("Error (YearFormat)");
				}
			}
		}
	}
	
	//sets the thumbnail and scales it down to a fixed size
	protected void setFrontCover(byte[] imageData){
		if(infoPanel.isAncestorOf(frontCover))infoPanel.remove(frontCover);
		ImageIcon icon=new ImageIcon(imageData);
		icon=rescale(icon);
		frontCover=new JButton(icon);
		frontCover.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				control.coverClicked();
			}
		});
		frontCover.setBorder(new EmptyBorder(0,0,0,0));
		infoPanel.setBorder (new EmptyBorder(10,10,10,10));
		infoPanel.add(frontCover,BorderLayout.SOUTH);
		infoPanel.revalidate();
	}
	//returns a rescaled version of the input with a fixed size	
	private ImageIcon rescale(ImageIcon icon){
		Image img=icon.getImage();
		img=img.getScaledInstance(COVER_WIDTH, COVER_HEIGHT, Image.SCALE_SMOOTH);
		icon.setImage(img);
		return icon;
	}
	
	//sets the current tree behind the jtree
	//adds listeners for selection(calls control.newTreeElementSelected)
	protected void setTree(MutableTreeNode top){
		if (tree!=null) this.getContentPane().remove(tree); 
		tree=new JTree(top);
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				if (node == null) return;
			    control.newTreeElementSelected(node); 
			}
		});
		this.getContentPane().add(tree,BorderLayout.CENTER);
	}
		
	protected void setSongTitle(String songTitle){
		this.songTitle.setText(songTitle);
	}
		
	protected void setInterpret(String interpret){
		this.interpret.setText(interpret);
	}
		
	protected void setAlbum(String album){
		this.album.setText(album);
	}
	
	//deletes leading zeros before the jtextfield entry is edited	
	protected void setYear(String year){
		int l=year.length();
		for (int i=0;i<l&&i<3;i++)
			if(year.charAt(0)=='0'){
				year=year.substring(1);
			}
		this.year.setText(year);
	}
		
	protected String getSongTitle(){
		return songTitle.getText();
	}
		
	protected String getInterpret(){
		return interpret.getText();
	}
		
	protected String getAlbum(){
		return album.getText();
	}
	//adds leading zeros(always returns a 4 digit number)	
	protected String getYear(){
		correctYearFormat();
		String temp=year.getText();
		for (int i=0;i<4-temp.length();i++){
			temp="0"+temp;
		}
		return temp;
	}
		
	public void keyPressed(KeyEvent k){
		this.correctYearFormat();
	}
		
	public void keyReleased(KeyEvent k){
		this.correctYearFormat();
	}
		
	public void keyTyped(KeyEvent k){
			
	}
}
